package net.zoneland.knowledge.utils;

import java.util.function.Function;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.zoneland.knowledge.controller.bean.OperationResult;

/**
 * 健康检查方法
 * @param <T>
 * @param <R>
 */
public class FunctionUtils<T,R> {
    private static final Logger LOGGER = LoggerFactory.getLogger(FunctionUtils.class);

    /**
     * 健康检查
     * @param function
     * @param t
     * @param type
     * @return
     */
    public OperationResult checkDataSource(Function<T, R> function, T t, String type) {
        OperationResult res = new OperationResult();
        res.setSuccess(true);
        try {
            R apply = function.apply(t);
        } catch (Exception e) {
            res.setSuccess(false);
            res.setMsg(e.getMessage());
            LOGGER.error("check {} error", type, e);
        }
        return res;
    }
}
